# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymysql


def dbHandle():
    conn = pymysql.connect(
        host='localhost',
        user='sqluser',
        passwd='sqlpasswd',
        charset='utf8',
        use_unicode=False
    )
    return conn


class ToutiaoPipeline(object):
    def process_item(self, item, spider):
        dbObject = dbHandle()
        cursor = dbObject.cursor()
        sql = 'insert ignore into qdm12852548_db.newsdata(title,time,fromurl,body) values (%s,%s,%s,%s)'

        try:
            cursor.execute(sql, (item['news_title'], item['news_time'], item['news_url'], item['news_body']))
        except Exception:
            print(Exception)
            dbObject.rollback()
        return item
